/// <reference types='vitest' />

import { defineConfig } from "vite"
import vue from "@vitejs/plugin-vue"
import AutoImport from "unplugin-auto-import/vite"
import path from "path"
// import babel from 'babe'
import jsx from "@vitejs/plugin-vue-jsx" // 支持tsx 语法
const Components = require("unplugin-vue-components/vite")
const { ElementPlusResolver } = require("unplugin-vue-components/resolvers")

import Unocss from "unocss/vite"
import { presetUno, presetAttributify, presetIcons } from "unocss"

import { viteMockServe } from "vite-plugin-mock"

// vite 默认只会编译ts 不会检测ts
export default defineConfig({
  build: {
    rollupOptions: {
      exclude: ['./data.json', './defaultOption.json']
    }
  },
  plugins: [
    // viteMockServe(),
    jsx(),
    // babel({
    //   presets: [
    //     '@babel/preset-env', "@vue/babel-preset-jsx"
    //   ],
    //   plugins: [
    //     "@babel/plugin-transform-runtime"
    //   ]
    // }),
    vue({
      // script:{
      //   refSugar
      // }
    }),
    Components({
      resolvers: [ElementPlusResolver()]
    }),
    AutoImport({
      imports: ["vue", "vue-router", "pinia"],
      resolvers: [ElementPlusResolver()]
      // 生成.eslintrc-auto-import.json
      // 生成一次后，可注释掉，每次保存都会生成一次很浪费性能
      // eslintrc: { enabled: true }
    }),
    Unocss({
      presets: [
        presetUno(),
        presetAttributify(),
        presetIcons({
          extraProperties: {
            display: "inline-flex",
            width: "2em",
            height: "2em",
            "vertical-align": "middel"
          },
          collections: {
            zf: {
              circle: `<svg width="50" height="50" viewBox="0 0 50 50">
                <circle cx="25" cy="25" r="20" />
              </svg>`
            }
          }
        })
      ]
    })
    // 默认使用的是windicss 支持将类名 用属性的方式来标识
    // 支持css icon
  ],
  resolve: {
    alias: [
      // 配置和rollup一样
      {
        find: "@",
        replacement: path.resolve(__dirname, "src")
      }
    ]
  },
  // test: {
  //   globals: true,
  //   environment: "happy-dom",
  //   transformMode: { web: [/.tsx$/] }
  // },
  server: {
    // 反向代理 不需要配置跨域 http://127.0.0.1:5173/api/login http://127.0.0.1:3000/login
    proxy: {
      // http-proxy 在中间做了一个中间层 客户端-> 中间层-> 真实服务器
      "/api": {
        target: "http://localhost:3000/",
        changeOrigin: true, // 这里不加服务端无法拿到origin属性
        rewrite: (path) => path.replace(/^\/api/, "")
      }
    }
  }
})
